<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="__ROOT__/Public/Js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="__ROOT__/Public/Js/core.js"></script>
  </head>
  <body>
    <div id="content">
      <volist name="list" id="volist" key="i">
        <ul>
          <li>
            <a href="javascript:void(0)" display="1" onclick="clickLink(this)">{$setting[$i]}</a>
          <if condition="$volist.check eq 1"><input type="checkbox" checked="checked" onclick="clickLevel1(this,'{$volist.ids}','{$i}')"/>
            <else/><input type="checkbox" onclick="clickLevel1(this,'{$volist.ids}','{$i}')" />
          </if>
          <ul>
            <volist name="volist.list" id="vol" key="j">
              <li>
                <a href="javascript:void(0)" display="0" onclick="clickLink(this)">{$vol.title}</a>
              <if condition="$vol.check eq 1"><input type="checkbox" checked="checked" onclick="clickLevel1(this,'{$vol.id}','{$i}')"/>
                <else/><input type="checkbox" onclick="clickLevel1(this,'{$vol.id}','{$i}')" />
              </if>
              <ul style="display:none" display="0">
                <volist name="vol.downLine" id="vo" key="k">
                  <li>
                    {$vo.title}
                  <if condition="$vo.check eq 1"><input type="checkbox" checked="checked" onclick="clickLevel2(this,'{$vo.id}','{$i}')"/>
                    <else/><input type="checkbox" onclick="clickLevel2(this,'{$vo.id}','{$i}')"/>
                  </if>
                  </li>
                </volist>
              </ul> 
              </li>
            </volist>
          </ul>
          </li>
        </ul>
      </volist>
      <form method="post" action="__URL__/settingSave">
        <input id="hids" type="hidden" name="hids" value="" />
        <input id="zids" type="hidden" name="zids" value="" />
        <input id="rids" type="hidden" name="rids" value="" />
        <input type="hidden" name="uid" value="{$uid}"/>
        <input type="submit" value="确认"/>
      </form>

    </div>
    <script>
      var userHZR = {$userHZR};
      var user = {1:[],2:[],3:[]};
      var hidden = {1:"hids",2:'zids',3:'rids'};
      for (var type in userHZR) {
        user[type] = userHZR[type].split(',');
        $("#"+hidden[type]).val(userHZR[type]);
      }
      function clickLink(self){
        var display = $(self).attr("display");
        var o = {0:{display:1,todo:"show"},1:{display:0,todo:"hide"}};
        eval('$(self).attr("display",'+o[display].display+').next().next("ul").'+o[display].todo+'()');
      }

      //可以把level1和level2两个函数合起来的
      function clickLevel1(self,ids,type){
        var check = $(self).attr('checked');
        ids = ids.split(',');
        $(self).next().find("input").attr('checked',check);
        if (check == true) {
          for (var i = 0; i < ids.length; i++) {
            user[type].push(ids[i]);
          }
          user[type] = formatArray(user[type]);
        }else {
          $(self).parent().parent().prev().attr('checked',false);
          for (var i = user[type].length - 1; i >= 0; i-- ) {
            for (var j = ids.length - 1; j >= 0; j--){
              if(user[type][i] == ids[j]) user[type].splice(i,1);
            }
          }
        }
        user[type].sort();
        $("#"+hidden[type]).val(user[type].join(','));
      }
      
      function clickLevel2(self,id,type){
        if ($(self).attr("checked") == true) {
          user[type].push(id);
        }else {
          $(self).parent().parent().prev().attr('checked',false);
          $(self).parent().parent().parent().parent().prev().attr('checked',false);
          for (var i = user[type].length - 1; i >= 0; i--) {
            if (user[type][i] == id) {
              user[type].splice(i,1);
              break;
            }
          }
        }
        user[type].sort();
        $("#"+hidden[type]).val(user[type].join(','));
      }

      /* 清除重复数组里面的重复元素，并且按照升序排列返回 */
      function formatArray(array){
        array.sort();
        for (var i = array.length - 1; i > 0; i--) {
          if (array[i] == array[i-1]) array.splice(i,1);
        }
        return array;
      }
    </script>
  </body>
</html>
